﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using DataLayer.Types;
using ViewModel.Finance;

namespace MyWpfApp.Finance
{
    /// <summary>
    /// Interaction logic for TheBudget.xaml
    /// </summary>
    public partial class TheBudget : UserControl
    {
        private TheBudgetViewModel viewModel;

        private GridViewColumnHeader sortHeader = null;
        private ListSortDirection sortDirection = ListSortDirection.Ascending;

        public TheBudget()
        {
            InitializeComponent();
        }

        private void NewItemClicked(object sender, RoutedEventArgs e)
        {
            BudgetItem item = new BudgetItem();
            ShowItemDialog(item);
        }

        private void ItemPropertiesClicked(object sender, RoutedEventArgs e)
        {
            ShowItemPropertiesDialog();
        }

        private void DeleteItem_Click(object sender, RoutedEventArgs e)
        {
            DeleteItem(listView.SelectedItem as BudgetItem);
        }

        private void listView_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ShowItemPropertiesDialog();
        }

        private void ShowItemPropertiesDialog()
        {
            BudgetItem item = listView.SelectedItem as BudgetItem;
            ShowItemDialog(item);
        }

        private void ShowItemDialog(BudgetItem item)
        {
            BudgetItemPropertiesView propertiesView = new BudgetItemPropertiesView();
            propertiesView.DataContext = new BudgetItemViewModel(item);
            propertiesView.ShowDialog();
        }

        private void DeleteItem(BudgetItem item)
        {
            MessageBoxResult result = MessageBox.Show("Are you sure you want to delete this item?\n\r\n\r" + item.Name,
                                                      "Delete Item", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
            if(result != MessageBoxResult.OK) return;

            viewModel.DeleteItem(item);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            viewModel = DataContext as TheBudgetViewModel;
        }


        private void ListViewEx_OnClick(object sender, RoutedEventArgs e)
        {
            GridViewColumnHeader columnHeader = e.OriginalSource as GridViewColumnHeader;
            ListSortDirection direction;
            if (columnHeader == null) return;
            if (columnHeader.Role == GridViewColumnHeaderRole.Padding) return;   //not actually a column

            // assign sort direction
            if (columnHeader == sortHeader)
            {
                direction = sortDirection == ListSortDirection.Ascending
                                ? ListSortDirection.Descending
                                : ListSortDirection.Ascending;
            }
            else
            {
                direction = ListSortDirection.Ascending;
            }

            //perform sort
            string headerName = columnHeader.Column.Header as string;
            if (headerName == null) return;
            Sort(headerName, direction);

            //carret symbol
            if (direction == ListSortDirection.Ascending)
            {
                columnHeader.Column.HeaderTemplate =
                    Resources["HeaderTemplateArrowUp"] as DataTemplate;
            }
            else
            {
                columnHeader.Column.HeaderTemplate =
                    Resources["HeaderTemplateArrowDown"] as DataTemplate;
            }

            // Remove arrow from previously sorted header
            if (sortHeader != null && sortHeader != columnHeader)
            {
                sortHeader.Column.HeaderTemplate = null;
            }

            sortHeader = columnHeader;
            sortDirection = direction;
        }


        private void Sort(string sortBy, ListSortDirection direction)
        {
            ICollectionView dataView = CollectionViewSource.GetDefaultView(listView.ItemsSource);

            dataView.SortDescriptions.Clear();
            SortDescription sd = new SortDescription(sortBy, direction);
            dataView.SortDescriptions.Add(sd);
            dataView.Refresh();
        }
    }
}
